﻿using System.Collections.Generic;
using System.Linq;
using System;
using Data;
using Entity;
using Entity.CommonEnums;
using Common.Background.Data;
using Common.Background.Entity;
namespace Common.Background.Service
{
    public class WebManageMenusServices : Service.ServiceBase, IWebManageMenusSerivces
    {

        #region 后台登录管理及菜单显示

        #region 得到用户职位信息
        /// <summary>
        /// 根据用户id及相关权限菜单id查询菜单信息
        /// </summary>
        /// <param name="menuid">用户id</param>
        /// <param name="menuid">菜单id</param>
        /// <returns>菜单实体</returns>
        public List<WebManageMenus> GetManageMenus(int userid, int menuid)//2010-10-20 修改一个身份拥有多个角色
        {
            var linqs = (from webmangemenus in iWebManageMenusRepository.GetModel().Where(i => i.Status != (int)Status.Hidden)
                         join wber_webm in iWebManageRole_WebManageMenusRepository.GetModel() on webmangemenus.MenuID equals wber_webm.MenuID
                         join webu_webr in iWebManageUser_WebManageRolesRepository.GetModel().Where(item => item.ManageUserID.Equals(userid)) on wber_webm.ManageRoleID equals webu_webr.ManageRoleID
                         orderby webmangemenus.SortNumber
                         select webmangemenus).Distinct();
            if (menuid != 0)
            {
                linqs = linqs.Where(menus => menus.ParentID.Equals(menuid));
            }
            return linqs.OrderBy(i => i.SortNumber).ToList();
        }
        #endregion

        #region 根据菜单节点查找是否有子节点
        /// <summary>
        /// 根据菜单节点查找是否有子节点
        /// </summary>
        /// <param name="menuid"></param>
        /// <returns></returns>
        public List<WebManageMenus> GetMenusChildNode(int userid, int menuid)
        {
            var linqs = (from webmangemenus in iWebManageMenusRepository.GetModel().Where(item => item.ParentID.Equals(menuid))
                         join wber_webm in iWebManageRole_WebManageMenusRepository.GetModel() on webmangemenus.MenuID equals wber_webm.MenuID
                         join webu_webr in iWebManageUser_WebManageRolesRepository.GetModel().Where(item => item.ManageUserID.Equals(userid)) on wber_webm.ManageRoleID equals webu_webr.ManageRoleID
                         orderby webmangemenus.SortNumber
                         select webmangemenus).Distinct();
            return linqs.OrderBy(i => i.SortNumber).ToList();
        }
        #endregion

        #region 用户登录
        public VMessage ManageuaerLogin(WebManageUsers user)
        {
            VMessage uLoginMsg = new VMessage();
            user.Password = VCommons.Encryptor.Utility.EncryptString(user.Password, VCommons.Encryptor.Utility.EncryptorType.MD5);
            WebManageUsers isui = iWebManageUsersRepository.UserLogin(user);

            if (isui == null)
            {
                uLoginMsg.AddItem("用户名或密码错误！");
            }
            else
            {
                isui.WebManageUser_WebManageRoles_Extend = iWebManageUser_WebManageRolesRepository.GetDetailModel().Where(i => i.ManageUserID.Equals(isui.ManageUserID));
                if (isui.WebManageUser_WebManageRoles_Extend == null)
                {
                    List<WebManageUser_WebManageRoles> uroles = new List<WebManageUser_WebManageRoles>();
                    uroles.Add(new WebManageUser_WebManageRoles { ManageUserID = isui.ManageUserID });
                    isui.WebManageUser_WebManageRoles_Extend = uroles.AsQueryable();
                }

                uLoginMsg.Entity = isui;
                uLoginMsg.IsComplete = true;
            }
            return uLoginMsg;
        }
        #endregion

        #region 修改密码
        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public Boolean UpdatePassword(WebManageUsers user, string password)
        {
            Boolean boo = false;
            try
            {
                WebManageUsers linqs = (from users in base.iWebManageUsersRepository.GetModel().Where(id => id.LoginName.Equals(user.LoginName) && id.Password.Equals(user.Password)) select users).FirstOrDefault();
                if (linqs != null)
                {
                    linqs.Password = password;
                    iWebManageUsersRepository.Update(linqs);
                    boo = true;
                }
            }
            catch (Exception)
            {
                boo = false;
            }
            return boo;
        }
        #endregion

        #region 读取用户角色
        /// <summary>
        /// 读取用户角色
        /// </summary>
        /// <param name="userid">用户id</param>
        /// <returns></returns>
        public List<WebManageRoles> GetUserRoles(string userid)
        {
            return (from userrolesid in iWebManageRolesRepository.GetModel()
                    join userrolee_menus in iWebManageUser_WebManageRolesRepository.GetModel().Where(item => item.ManageUserID.Equals(userid)) on userrolesid.ManageRoleID equals userrolee_menus.ManageRoleID
                    select userrolesid).ToList();
        }
        #endregion
        #endregion
    }
}
